package com.jedi;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Locale;





public class FormatDate {
	private java.util.Date currentDate = null;

	private SimpleDateFormat smpDateFormat = null;

	public FormatDate() {
		
		currentDate = new java.util.Date();
	}

	/**
	 * @return String (format:yy-mm-dd)
	 */
	public String rigorToDay() {
		smpDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return smpDateFormat.format(currentDate);
	}

	/**
	 * @return String (format:yy-mm-dd hh:mm)
	 */
	public String rigorToMin() {
		smpDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return smpDateFormat.format(currentDate);
	}

	/**
	 * @return String (format:yy-mm-dd hh:mm:ss)
	 */
	public String rigorToSec() {
		smpDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return smpDateFormat.format(currentDate);
	}

	/**
	 * @return String (format:YYMM)
	 */
	public String tightToMon() {
		smpDateFormat = new SimpleDateFormat("yyMM");
		return smpDateFormat.format(currentDate);
	}

	/**
	 * @return String (format:YYMMDD)
	 */
	public String tightToDay() {
		smpDateFormat = new SimpleDateFormat("yyMMdd");
		return smpDateFormat.format(currentDate);
	}

	/**
	 * @return String (format:YYMMDD)
	 */
	public String tightToDay(String aDate) {
		// smpDateFormat = new SimpleDateFormat("yyMMdd");
		try {
			smpDateFormat = new SimpleDateFormat();
			smpDateFormat.applyPattern("yyyy-MM-dd");
			smpDateFormat.setLenient(false);
			java.util.Date date = smpDateFormat.parse(aDate);
			smpDateFormat = new SimpleDateFormat("yyMMdd");
			return smpDateFormat.format(date);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * @return java.sql.Timestamp (format:yy-MM-dd hh:ss)
	 */
	public java.sql.Timestamp toTimestamp() {
		return new java.sql.Timestamp(new java.util.Date().getTime());
	}

	/**
	 * @return java.sql.Date (format:yy-MM-dd)
	 */
	public java.sql.Date toDate() {
		return new java.sql.Date(new java.util.Date().getTime());
	}

	/**
	 * @param String
	 * @return java.sql.Date (format:yy-MM-dd)
	 */
	public java.sql.Date toDate(String date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		try {
			dateFormat.applyPattern("yyyy-MM-dd");
			java.util.Date vDate = dateFormat.parse(date);
			return new java.sql.Date(vDate.getTime());
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * @param String
	 * @return java.sql.Date (format:yy-MM-dd)
	 */
	public String rigorMin(String date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		try {
			dateFormat.applyPattern("yyyy-MM-dd HH:mm");
			java.util.Date vDate = dateFormat.parse(date);
			return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(vDate);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * @param String
	 * @return java.sql.Date (format:yy-MM-dd)
	 */
	public String rigorSec(String date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		try {
			dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
			java.util.Date vDate = dateFormat.parse(date);
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(vDate);
		} catch (ParseException e) {
			return null;
		}
	}

    public static Date sqlData(java.util.Date d) {
        return new java.sql.Date(d.getTime());
    }
    
    public static Date USDate(String S) throws ParseException {
        SimpleDateFormat smpDateFormat = new SimpleDateFormat("dd-MMM-yyyy",  Locale.US);
        return sqlData(smpDateFormat.parse(S));
    }
    
	public static void main(String[] args) {
		FormatDate formatDate1 = new FormatDate();
		System.out.println(formatDate1.rigorToDay());
		System.out.println(formatDate1.rigorToMin());
		System.out.println(new FormatDate().toDate("2003-03-01"));
	}

}
